Imports System
Imports System.Web
Imports System.Net.Mail
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports DotNetNuke
Imports DotNetNuke.Common
Imports DotNetNuke.Security
Imports DotNetNuke.Framework
Namespace DNNQuickForm
    Partial Public Class FormBuilder
        Inherits DotNetNuke.Entities.Modules.PortalModuleBase
        Implements Entities.Modules.IActionable
        Protected WithEvents plhButton As System.Web.UI.WebControls.PlaceHolder
        Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
            Get
                Dim Actions As New Entities.Modules.Actions.ModuleActionCollection
                Actions.Add(GetNextActionID, "Manage Forms", Entities.Modules.Actions.ModuleActionType.AddContent, "", "", EditUrl(), False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
                Return Actions
            End Get
        End Property
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                Dim _LoadForm As String
                Dim FormName As String = GetString("DNNQF_FormName", String.Empty)
                If String.IsNullOrEmpty(FormName) Then
                    _LoadForm = "~/DesktopModules/DNNQuickForm/ContactForm.ascx"
                Else
                    _LoadForm = PortalSettings.HomeDirectory & "/DNNQuickForms/" & FormName
                End If
                Dim ctl As Control
                ctl = LoadControl(_LoadForm)
                ctl.ID = "AMForm"
                pnlForm.Controls.Add(ctl)
                While Not ctl.Controls.Count = 0
                    pnlForm.Controls.Add(ctl.Controls.Item(0))
                End While
                LinkControls(pnlForm.Controls)
                Dim btnSubmit As New LinkButton
                btnSubmit.ID = "btnSubmit"
                btnSubmit.Text = GetString("DNNQF_SubmitText", "Submit")
                btnSubmit.CssClass = GetString("DNNQF_SubmitCSSClass", "dnnPrimaryAction")
                btnSubmit.CausesValidation = True
                AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
                plhButton = pnlForm.FindControl("plhButton")
                If Not plhButton Is Nothing Then
                    plhButton.Controls.Add(btnSubmit)
                Else
                    pnlForm.Controls.Add(btnSubmit)
                End If
            Catch exc As Exception
                DotNetNuke.Services.Exceptions.ProcessModuleLoadException(Me, exc)
            End Try
        End Sub
        Private Function GetString(key As String, defaultValue As String) As String
            If (Settings.ContainsKey(key)) Then
                If Not String.IsNullOrEmpty(Settings(key).ToString()) Then
                    Return Settings(key).ToString()
                End If
            End If
            Return defaultValue
        End Function
        Private Function GetBoolean(key As String, defaultValue As Boolean) As Boolean
            If (Settings.ContainsKey(key)) Then
                If Not String.IsNullOrEmpty(Settings(key).ToString()) Then
                    Return CType(Settings(key), Boolean)
                End If
            End If
            Return defaultValue
        End Function
        Public Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Page.Validate()
            Dim dnnCaptcha As DotNetNuke.UI.WebControls.CaptchaControl = FindControl("ctlCaptcha")
            Dim captchaValid As Boolean = True
            If Not dnnCaptcha Is Nothing Then
                captchaValid = dnnCaptcha.IsValid
            End If
            If Page.IsValid And captchaValid Then
                Dim sb As New Text.StringBuilder
                Dim sSubject As String = GetString("DNNQF_Subject", PortalSettings.PortalName & " Contact Form")

                Dim sTo As String = GetString("DNNQF_To", PortalSettings.Email)
                Dim sCC As String = GetString("DNNQF_CC", String.Empty)
                Dim sBCC As String = GetString("DNNQF_BCC", String.Empty)
                Dim sHeader As String = GetString("DNNQF_Header", String.Empty)
                Dim sFooter As String = GetString("DNNQF_Footer", String.Empty)
                Dim bAddIP As Boolean = GetBoolean("DNNQF_AddIPAddress", True)
                Dim bAddUserAgent As Boolean = GetBoolean("DNNQF_AddUserAgent", True)
                Dim bAddTimeStamp As Boolean = GetBoolean("DNNQF_AddTimeStamp", True)
                Dim sRedirect As String = GetString("DNNQF_Redirect", String.Empty)
                Dim sThankyou As String = GetString("DNNQF_Thankyou", "<b>Thank you!</b>")

                Dim ctlUserControl As Control = FindControl("AMForm")
                If Not ctlUserControl Is Nothing Then
                    sb.Append("<table>")
                    Dim ctl As Control
                    If pnlForm.Controls.Count > 0 Then
                        For Each ctl In pnlForm.Controls
                            If TypeOf ctl Is HtmlInputText Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, HtmlInputText).ID.ToString.Replace("_", " ") & ":</td><td valign=top>" & DirectCast(ctl, HtmlInputText).Value & "</td></tr>")
                            End If
                            If TypeOf ctl Is TextBox Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, TextBox).ID.ToString.Replace("_", " ") & ":</td><td valign=top>" & DirectCast(ctl, TextBox).Text & "</td></tr>")
                            End If
                            If TypeOf ctl Is HtmlTextArea Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, HtmlTextArea).ID.ToString.Replace("_", " ") & ":</td><td valign=top>" & DirectCast(ctl, HtmlTextArea).InnerText & "</td></tr>")
                            End If
                            If TypeOf ctl Is CheckBox Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, CheckBox).ID.ToString.Replace("_", " ") & ":</td><td valign=top>" & DirectCast(ctl, CheckBox).Checked & "</td></tr>")
                            End If
                            If TypeOf ctl Is DropDownList Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, DropDownList).ID.ToString.Replace("_", " ") & ":</td><td valign=top>" & DirectCast(ctl, DropDownList).SelectedItem.Value & "</td></tr>")
                            End If
                            If TypeOf ctl Is CheckBoxList Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, CheckBoxList).ID.ToString.Replace("_", " ") & ":</td><td valign=top>")
                                Dim li As ListItem
                                For Each li In DirectCast(ctl, CheckBoxList).Items
                                    If li.Selected = True Then
                                        sb.Append(li.Text & "<br>")
                                    End If
                                Next
                                sb.Append("</td></tr>")
                            End If
                            If TypeOf ctl Is ListBox Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, ListBox).ID.ToString.Replace("_", " ") & ":</td><td valign=top>")
                                Dim li As ListItem
                                For Each li In DirectCast(ctl, ListBox).Items
                                    If li.Selected = True Then
                                        sb.Append(li.Text & "<br>")
                                    End If
                                Next
                                sb.Append("</td></tr>")
                            End If
                            If TypeOf ctl Is RadioButtonList Then
                                sb.Append("<tr><td valign=top>" & DirectCast(ctl, RadioButtonList).ID.ToString.Replace("_", " ") & ":</td><td valign=top>")
                                Dim li As ListItem
                                For Each li In DirectCast(ctl, RadioButtonList).Items
                                    If li.Selected = True Then
                                        sb.Append(li.Text & "<br>")
                                    End If
                                Next
                                sb.Append("</td></tr>")
                            End If
                        Next
                    End If

                    sb.Append("</table>")
                End If
                Try
                    sb.Insert(0, sHeader)
                    If bAddIP = True Then
                        sb.Append("User IP Address: " & Request.UserHostAddress & "<br>")
                    End If
                    If bAddUserAgent = True Then
                        sb.Append("User Browser: " & Request.UserAgent & "<br>")
                    End If
                    If bAddTimeStamp = True Then
                        sb.Append("Date/Time Submitted:" & Now.ToString & "<br>")
                    End If
                    sb.Append(sFooter)
                    Dim Email As New MailMessage
                    Email.To.Add(New MailAddress(sTo))
                    Email.From = New MailAddress(PortalSettings.Email)
                    If Not String.IsNullOrEmpty(sCC) Then
                        Email.CC.Add(New MailAddress(sCC))
                    End If
                    If Not String.IsNullOrEmpty(sBCC) Then
                        Email.Bcc.Add(sBCC)
                    End If
                    Email.Subject = sSubject
                    Email.Body = sb.ToString
                    Email.IsBodyHtml = True
                    Dim SmtpServer As String = DotNetNuke.Entities.Host.Host.SMTPServer
                    Dim SmtpUserName As String = DotNetNuke.Entities.Host.Host.SMTPUsername
                    Dim SmtpPassword As String = DotNetNuke.Entities.Host.Host.SMTPPassword
                    Dim SmtpSSL As Boolean = DotNetNuke.Entities.Host.Host.EnableSMTPSSL
                    Dim client As New System.Net.Mail.SmtpClient
                    If (SmtpSSL) Then
                        client.EnableSsl = True
                    End If
                    If Not String.IsNullOrEmpty(SmtpServer) Then
                        Dim portPos As Integer = SmtpServer.IndexOf(":")
                        If portPos > -1 Then
                            client.Port = Convert.ToInt32(SmtpServer.Substring(portPos + 1, SmtpServer.Length - portPos - 1))
                            SmtpServer = SmtpServer.Substring(0, portPos)
                        End If
                        client.Host = SmtpServer
                    End If
                    If Not String.IsNullOrEmpty(SmtpUserName) AndAlso Not String.IsNullOrEmpty(SmtpPassword) Then
                        client.Credentials = New System.Net.NetworkCredential(SmtpUserName, SmtpPassword)
                    End If
                    Try
                        client.Send(Email)
                    Catch ex As Exception
                        DotNetNuke.Services.Exceptions.LogException(ex)
                    End Try
                    If Not sRedirect = "" Then
                        Response.Redirect(sRedirect)
                    Else
                        pnlForm.Controls.Clear()
                        pnlForm.Visible = False
                        lblOutput.Text = sThankyou
                        lblOutput.Visible = True
                    End If
                Catch ex As Exception
                    lblOutput.Text = ex.ToString
                End Try
            End If
        End Sub
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                Select Case ctrl.ID
                    Case "plhButton"
                        plhButton = CType(ctrl, PlaceHolder)
                End Select
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
    End Class
End Namespace